package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationBO;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationUpdateBO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.shop.ShopMemberRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopMemberRelationService;
import com.ztgf.shopping.models.vo.shop.ShopMemberRelationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 店铺会员表后台接口类
 *
 * @author zhanghualang
 * @Date 2020-05-26
 */

@RestController
@RequestMapping("/api/shopMemberRelation")
@Slf4j
@Api(value = "ShopMemberRelationController",tags = "店铺会员表接口")
public class ShopMemberRelationApiController {
    @DubboReference
    private IShopMemberRelationService IShopMemberRelationService;

    @DubboReference
    private IShopInfoService iShopInfoService;

    @ApiOperation(value = "添加店铺会员表",notes = "添加店铺会员表")
    @PostMapping(value = "/insert")
    public Result insert( ShopMemberRelationSaveBO insertSaveBO) {
        //验证用户是否存在
        ShopInfoDTO shopInfoDTO = iShopInfoService.getByMobileName(insertSaveBO.getCompanyName(), insertSaveBO.getMobile());
        if (shopInfoDTO == null){
            return  new Result<>(CodeEnum.FAIL_100036);
        }

        insertSaveBO.setBuyShopId(shopInfoDTO.getId());
        //验证是否已经添加
        //状态 0正常，1删除。默认0
        if (IShopMemberRelationService.updateByShopId(insertSaveBO.getSellShopId(), insertSaveBO.getBuyShopId(), 0)){
            return new Result<>(CodeEnum.FAIL_100037);
        }

        boolean flag = IShopMemberRelationService.add(insertSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改店铺会员表",notes = "修改店铺会员表")
    @PostMapping("/update")
    public Result update(ShopMemberRelationUpdateBO updateSaveBO){
        ShopMemberRelationDTO shopMemberRelationDTO = new ShopMemberRelationDTO();
        BeanUtils.copyProperties(updateSaveBO, shopMemberRelationDTO);
        shopMemberRelationDTO.setId(updateSaveBO.getId());
        boolean flag = IShopMemberRelationService.updateById(shopMemberRelationDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态,删除会员",notes = "修改状态，删除会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    })
    public Result updateStatus(Long id){
        ShopMemberRelationDTO dto = new ShopMemberRelationDTO();
        dto.setId(id);
        dto.setStatus(1);//状态 0正常，1删除。默认0
        boolean flag = IShopMemberRelationService.updateById(dto);
        if(flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @PostMapping("/updateStatusBatch")
    @ApiOperation(value = "批量修改状态，删除会员",notes = "批量修改状态，删除会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    })
    public Result updateStatusBatch(String ids){
        String[] arr = ids.split(",");
        ArrayList<ShopMemberRelationDTO> dtos = new ArrayList<ShopMemberRelationDTO>();
        for (String id : arr){
            ShopMemberRelationDTO dto = new ShopMemberRelationDTO();
            dto.setId(Long.valueOf(id));
            dto.setStatus(1);//状态 0正常，1删除。默认0
            dtos.add(dto);
        }

        boolean flag = IShopMemberRelationService.updateBatchById(dtos);
        if(flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找店铺会员表",notes = "根据id查找店铺会员表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopMemberRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopMemberRelationDTO shopMemberRelationDTO = IShopMemberRelationService.selectById(id);
        return new Result<>(shopMemberRelationDTO);
    }

    @ApiOperation(value = "店铺会员表列表",notes = "店铺会员表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopMemberRelationVO>> queryPageList(ShopMemberRelationBO shopMemberRelationBO){
        Page<ShopMemberRelationVO> page = IShopMemberRelationService.queryMemberPageList(shopMemberRelationBO);
        return new Result<>(page);
    }
}
